<template>
	<view class="type-container u-flex">
		<u-navbar title="分类" placeholder> </u-navbar>
		<view class="level-type-container u-flex">
			<view class="l-box">
				<scroll-view scroll-y scroll-with-animation class="tab-list1" :show-scrollbar="false"
					:scroll-into-view="'item1-' + scrollItem1">
					<view v-for="(item, index) in list1" :key="item.id" class="tab-item1 u-flex-xy-center"
						:class="activeitem1 == index ? 'active' : ''" :id="'item1-' + index" @click="list1tap(index)">
						{{ item.fullName }}
					</view>
				</scroll-view>
			</view>
			<view class="r-box">
				<!-- 	<scroll-view scroll-x scroll-with-animation class="right-t-box" :show-scrollbar="false"
					:scroll-into-view="'item2-' + scrollItem2">
					<view class="u-flex-y-center" style="width: auto; height: 100rpx">
						<view v-for="(item, index) in list2" :key="item.id" :id="'item2-' + index">
							<u-tag :text="item.fullName" plain shape="circle" type="info"
								:bgColor="activeitem2 == index ? 'rgba(76,217,100,0.13)' : '#fff'"
								:borderColor="activeitem2 == index ? '#39C03E' : '#fff'"
								:color="activeitem2 == index ? '#39C03E' : '#333'" @click="list2tap(index)"></u-tag>
						</view>

					</view>
				</scroll-view> -->
				<scroll-view :scroll-top="scrollTop" scroll-y class="right-b-box" :show-scrollbar="false" enhanced
					:refresher-enabled="activeitem1 != 0" refresher-background="#F4F4F4"
					:scroll-into-view="'item3-' + scrollItem3" :refresher-triggered="triggered3"
					@refresherrefresh="list3refresherpulling" :lower-threshold="0" @scrolltolower="list3scrolltolower">
					<view style="min-height: calc(100vh - 88rpx - 100rpx);">
						<view v-for="(item2, index2) in list2" :key="item2.id" v-if="list3[index2].length != 0"
							:id="'item3-' + index2">
							<!-- <view class="tab-item2 u-flex-y-center">
								{{ item2.fullName }}
							</view> -->
							<u-divider :text="item2.fullName" textColor="#333"></u-divider>
							<view class="tab-box3">
								<view class="tab-item3 u-flex-xy-center" v-for="(item3, index3) in list3[index2]"
									:key="item3.id" @click="list3tagTap(list1[activeitem1], item2, item3)">
									<view class="logoUrl u-flex-xy-center">
										<u--image :src="item3.logoUrl" width="88rpx" height="88rpx"
											radius="10rpx"></u--image>
									</view>
									<view class="list3fullName u-flex-xy-center u-line-1">
										{{ item3.fullName }}
									</view>
									<!-- <view class="u-line-2" style="text-align: center">
                    {{ item3.fullName }}
                  </view> -->
								</view>
							</view>
						</view>
					</view>
					<view class="slll u-flex-xy-center" v-if="list1[activeitem1 + 1]">
						向上继续拉浏览 {{ list1[activeitem1 + 1].fullName }}
					</view>
					<view class="" v-if="list3scrolltoloweri != 0" style="height: 40rpx">
					</view>
					<u-safe-bottom></u-safe-bottom>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getProductClassAll
	} from "@/api/index.js";
	export default {
		name: "type",
		data() {
			return {
				list: [],
				list1: [],
				activeitem1: 0,
				scrollItem1: 0,
				list2: [],
				activeitem2: 0,
				scrollItem2: 0,
				list3: [],
				scrollItem3: 0,
				triggered3: false,
				scrollTop: 0,

				list3scrolltoloweri: 0,
				query: {}
			};
		},
		onLoad(query) {
			// level1Type
			this.query = query;
			this.init();
		},
		methods: {
			async init() {
				const res = await getProductClassAll();
				this.list = res.data;
				this.list1 = this.list.filter((e) => {
					return e.parentId == 0;
				});
				if (this.query.level1Type) {
					let index = this.list1.findIndex(e => e.id == this.query.level1Type)
					if (index != -1) {
						this.list1tap(index);
						return
					}
				}
				if (this.list1.length != 0) {
					this.list1tap(0);
				}
			},
			list1tap(index) {
				console.log(111);
				if (index >= this.list1.length || index < 0) {
					return;
				}
				this.activeitem1 = index;
				this.list3scrolltoloweri = 0;
				this.list2 = this.list.filter((e) => {
					return e.parentId == this.list1[this.activeitem1].id;
				});
				this.list3 = this.list2.map((e) => {
					return this.list.filter((e2) => {
						return e2.parentId == e.id;
					});
				});
				this.activeitem2 = 0;
				this.scrollItem3 = 0;
				// 异步更新滚动位置
				this.$nextTick(() => {
					this.scrollTop = this.scrollTop === 0 ? 1 : 0;
				});
			},
			list3tagTap(item1, item2, item3) {
				// calhost:8080/#/pages/shop/goods/list/list
				uni.setStorageSync('goodsListKey', {
					fullName: item3.fullName
				});
				uni.navigateTo({
					url: `/pages/shop/goods/list/list?level1Type=${item1.id}&level2Type=${item2.id}&level3Type=${item3.id}`,
				});
				// this.$emit("click", {
				//   item1,
				//   item2,
				//   item3,
				// });
			},
			list2tap(index) {
				this.activeitem2 = index;
				this.scrollItem3 = index;
				// this.tabCur = index;
			},
			list3refresherpulling() {
				this.triggered3 = true;
				setTimeout(() => {
					this.triggered3 = false;
					setTimeout(() => {
						this.list1tap(this.activeitem1 - 1);
					}, 300)
				}, 300);
			},

			list3scrolltolower() {
				console.log("滚动到底部", this.list3scrolltoloweri);
				if (this.activeitem1 == this.list1.length - 1) {
					return
				}
				if (
					this.list3scrolltoloweri >= 1
				) {
					this.list1tap(this.activeitem1 + 1);
				} else {
					uni.$u.debounce(this.list3scrolltolowerfn, 500)
				}

				// activeitem1
			},
			list3scrolltolowerfn() {
				this.list3scrolltoloweri++;
				console.log("滚动到底部", this.list3scrolltoloweri);

			}
		},
	};
</script>

<style lang="scss" scoped>
	.type-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		width: 100vw;
		overflow: hidden;
	}

	.level-type-container {
		// width: 100%;

		height: 0;
		flex: 1;

		.l-box {
			width: 200rpx;
			height: 100%;
			background: #f5f5f5;

			.tab-list1 {
				height: 100%;

				.tab-item1 {
					height: 80rpx;
					font-size: 24rpx;

					&.active {
						background: #ffffff;
						color: #39c03e;
						border-left: 8rpx solid #39c03e;
					}
				}
			}
		}

		.r-box {
			width: 0;
			flex: 1;
			background: #ffffff;
			display: flex;
			flex-direction: column;

			.right-t-box {
				height: 100rpx;
				padding: 0 20rpx;
			}

			.right-b-box {
				flex: 1;
				height: 0;

				.slll {
					color: #6d6d6d;
					font-size: 24rpx;
					height: 80rpx;
				}

				.tab-item2 {
					padding: 0 20rpx;
					font-size: 28rpx;
					height: 60rpx;
				}

				.tab-box3 {
					padding: 0 20rpx;
					display: flex;
					flex-wrap: wrap;
					justify-content: space-between;


					.tab-item3 {
						display: flex;
						flex-direction: column;

						width: 160rpx;
						// height: 80rpx;
						// background: #f5f5f5;
						font-size: 24rpx;
						margin-bottom: 10rpx;
						border-radius: 10rpx;
						// padding: 0 20rpx;


						.list3fullName {
							padding: 0 10rpx;
							margin-top: 10rpx;
							margin-bottom: 10rpx;
							font-size: 24rpx;
							color: #333;
							text-align: center;
						}
					}
				}
			}
		}
	}
</style>